#include "button_driver.h"

static const char *TAG = "button_driver";
QueueHandle_t button_queue;

static void IRAM_ATTR button_isr_handler(void *arg){
    bool button_press = true;
    xQueueSendFromISR(button_queue, &button_press, NULL);
}

void buttton_init(void){
    gpio_config_t io_conf = {
        .pin_bit_mask = (1ULL << BUTTON_GPIO),
        .mode = GPIO_MODE_INPUT,
        .pull_up_en = GPIO_PULLUP_ENABLE,
        .intr_type = GPIO_INTR_NEGEDGE};

    gpio_config(&io_conf);

    button_queue = xQueueCreate(5, sizeof(bool));
    gpio_install_isr_service(0);
    gpio_isr_handler_add(BUTTON_GPIO, button_isr_handler, NULL);
}

bool button_wait_press(uint32_t timeout_ms){
    bool button_press = false;
    xQueueReceive(button_queue, &button_press, timeout_ms / portTICK_PERIOD_MS);
    return true;
}